package cn.santi.excel;

import cn.santi.domain.JAppointment;
import cn.santi.domain.JUser;
import com.alibaba.excel.EasyExcel;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

public class ExcelExportUtil {

    public static void exportExcel(HttpServletResponse response, List<JUser> data) throws IOException {
        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("Data", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

        // 写入响应输出流
        EasyExcel.write(response.getOutputStream(), JUser.class)
                .sheet("Sheet1") // 这里可以指定工作表的名称
                .doWrite(data);
    }

    public static void sexportExcel(HttpServletResponse response, List<JAppointment> data) throws IOException {
        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("Data", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

        // 写入响应输出流
        EasyExcel.write(response.getOutputStream(), JAppointment.class)
                .sheet("Sheet1") // 这里可以指定工作表的名称
                .doWrite(data);
    }
}